package com.dongdongshop.controller;

import com.dk.enums.ResultEnum;
import com.dk.util.PageResult;
import com.dk.data.R;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//品牌管理表
@Controller
@RequestMapping("brand")
public class BrandController {

    @DubboReference
    BrandService bs;

    //查询全部跳转
    @RequestMapping("list")
    public String list(){
        return "admin/brand";
    }


    //查询全部
    @RequestMapping("getall")
    @ResponseBody
    public Map<String,Object> getall(@RequestParam(defaultValue = "1") Integer pagenum,@RequestParam(defaultValue = "4") Integer pagesize){
        PageResult P = bs.getall(pagenum, pagesize);
        Map<String,Object> m=new HashMap<>();
        m.put("total", P.getTotal());
        m.put("rows", P.getRows());
        return m;
    }


    //增加跳转
    @RequestMapping("addtiao")
    public String addtioa(){
        return "/brand/addbrand";
    }

    //增加
    @RequestMapping("addbrand")
    @ResponseBody
    public int addbrand(TbBrand tb){
        return bs.addbrand(tb);
    }


    //修改回显
    @RequestMapping("updatetiao")
    public String updatetiao(Model mo,Long id){
//        mo.addAttribute("b",bs.byid(id));
       TbBrand b= bs.byid(id);
       mo.addAttribute("b",b);
        return "/brand/updatebrand";
    }

    //修改
    @RequestMapping("updatebrand")
    @ResponseBody
    public int updatebrand(TbBrand tb){
        return bs.updatebrand(tb);
    }

    //删除
    @RequestMapping("deleteid")
    @ResponseBody
    public int deleteid(Long id){
        return bs.deleteid(id);
    }


    //状态码方式
    //查询全部
    @RequestMapping("getlist")
    @ResponseBody
    public R<List<TbBrand>> getlist(){
        List<TbBrand> ls = bs.getlist();
        return R.ok().setData(ls);
    }


    //状态码查询
    @RequestMapping("getname")
    @ResponseBody
    public R<List<TbBrand>> getname(String name){
        if(name==null ||name.equals("")){
            return R.response(ResultEnum.ARGS_ISNULL);
        }
        List<TbBrand> ls = bs.getlist();
        return R.ok().setData(ls);
    }


    //状态码分页查询
    @RequestMapping("selectfen")
    @ResponseBody
    public R<List<TbBrand>> selectfen(@RequestParam(defaultValue = "1") Integer pagenum,
                                      @RequestParam(defaultValue = "4") Integer pagesize,String name){
        PageResult p=bs.selectfen(pagenum,pagesize,name);
        return R.ok().setData(p);
    }



    //状态码方式增加
    @RequestMapping("add")
    @ResponseBody
    public R add(TbBrand tb){
       try {
             bs.addbrand(tb);
       }catch (Exception e){
           e.printStackTrace();
          return R.error();//返回失败

       }
       return R.ok();//返回成功
    }




}
